package cn.edu.dutir.model.vsm.old;

import java.io.File;
import java.io.IOException;
import java.io.ObjectOutput;
import java.util.Map;
import java.util.Set;

import cn.edu.dutir.model.vsm.Document;
import cn.edu.dutir.utility.Constants;

import com.aliasi.corpus.TextHandler;
import com.aliasi.symbol.MapSymbolTable;
import com.aliasi.util.FeatureExtractor;
import com.aliasi.util.Files;
import com.aliasi.util.ObjectToCounterMap;

public class CorpusStatistorHandler<E> implements TextHandler {

	final FeatureExtractor<? super E> mFeatureExtractor;
	MapSymbolTable mFileSymbolTable;
	MapSymbolTable mTokenSymbolTable;
	ObjectToCounterMap<Integer> mDFTable;
	ObjectOutput mObjectOut;

	public CorpusStatistorHandler(FeatureExtractor<E> fExtractor,
			MapSymbolTable tokenSymbolTable, MapSymbolTable fileSymbolTable,
			ObjectToCounterMap<Integer> dfTable, ObjectOutput objectOut) {
		mFeatureExtractor = fExtractor;
		mTokenSymbolTable = tokenSymbolTable;
		mFileSymbolTable = fileSymbolTable;
		mDFTable = dfTable;
		mObjectOut = objectOut;
	}

	public FeatureExtractor<? super E> featureExtractor() {
		return mFeatureExtractor;
	}

	public MapSymbolTable fileSymbolTable() {
		return mFileSymbolTable;
	}

	public MapSymbolTable tokenSymbolTable() {
		return mTokenSymbolTable;
	}

	public ObjectToCounterMap<Integer> docFrequencyMap() {
		return mDFTable;
	}

	public void dispatcher(File in) throws IOException {
		dispatcher(in, Constants.DEFAULT_CHARSET);
	}

	public void dispatcher(File in, String charset) throws IOException {
		visit(in, charset);
	}

	protected void visit(File in) throws IOException {
		visit(in, Constants.DEFAULT_CHARSET);
	}
	
	protected void visit(File in, String charset) throws IOException {
		if (in.isDirectory()) {
			String files[] = in.list();
			for (String file : files) {
				visit(new File(in, file), charset);
			}
		} else {
			E fileContent = (E) Files.readFromFile(in, charset);
			int id = mFileSymbolTable.getOrAddSymbol(in.getCanonicalPath());
			Map<String, ? extends Number> featureMap = mFeatureExtractor
					.features(fileContent);
			Document doc = new Document(in.getCanonicalPath(), featureMap);
			doc.compileTo(mObjectOut);
			handle(doc);
		}
	}

	public void handle(Document doc) {
		Set<String> featureSet = doc.termFrequency().keySet();
		for (String feature : featureSet) {
			int tokenId = mTokenSymbolTable.getOrAddSymbol(feature);
			mDFTable.increment(tokenId);
		}
	}

	@Override
	public void handle(char[] cs, int start, int length) {
		String text = new String(cs, start, length);
	}
}
